#include "qwdialogpen.h"
#include "ui_qwdialogpen.h"
#include<QColor>
#include<QColorDialog>

extern "C" int Add(int a,int b)
{
    return a+b;
}

QwDialogPen::QwDialogPen(QDialog *parent)
    : QDialog(parent)
    , ui(new Ui::QwDialogPen)
{
    ui->setupUi(this);
    // 线形CombBox选择项
    ui->comboBox_LineShape->clear();
    ui->comboBox_LineShape->addItem("NoPen",0);
    ui->comboBox_LineShape->addItem("SolidLine",1);
    ui->comboBox_LineShape->addItem("DashLine",2);
    ui->comboBox_LineShape->addItem("DotLine",3);
    ui->comboBox_LineShape->addItem("DashDotLine",4);
    ui->comboBox_LineShape->addItem("DashDotDotLine",5);
    ui->comboBox_LineShape->addItem("CustomDashLine",6);

    ui->comboBox_LineShape->setCurrentIndex(1);

    connect(ui->btnOK,SIGNAL(clicked()),this,SLOT(accept()));

}

QwDialogPen::~QwDialogPen()
{
    delete ui;
}

void QwDialogPen::setPen(QPen pen)
{
    m_Pen = pen;
    ui->spinBox_LineWidth->setValue(pen.width());
    int nIndex = static_cast<int>(pen.style());
    ui->comboBox_LineShape->setCurrentIndex(nIndex);

    QColor color = pen.color();
    ui->btnLineColor->setAutoFillBackground(true);
    QString strText = QString::asprintf("background-color: rgb(%d, %d, %d);",
                                        color.red(),color.blue(),color.blue());
    ui->btnLineColor->setStyleSheet(strText);

}

QPen QwDialogPen::getPen()
{
    m_Pen.setStyle(Qt::PenStyle(ui->comboBox_LineShape->currentIndex()));
    m_Pen.setWidth(ui->spinBox_LineWidth->value());
    QColor color;
    color = ui->btnLineColor->palette().color(QPalette::Button);
    m_Pen.setColor(color);
    return m_Pen;
}

QPen QwDialogPen::getPen(QPen iniPen, bool &bOK)
{
    QwDialogPen* pDlg = new  QwDialogPen();
    pDlg->setPen(iniPen);
    QPen pen;
    int nRtn = pDlg->exec();

    if(nRtn == QDialog::Accepted)
    {
        pen = pDlg->getPen();
        bOK = true;
    }
    else
    {
        pen = iniPen;
        bOK = false;
    }

    delete pDlg;
    return pen;
}

void QwDialogPen::on_btnLineColor_clicked()
{
    QColor color = QColorDialog::getColor();
    if(color.isValid())
    {
        //用样式表设置QPushButton的背景色
        QString strText = QString::asprintf("background-color: rgb(%d, %d, %d);",
                                            color.red(),color.blue(),color.blue());
        ui->btnLineColor->setStyleSheet(strText);
    }
}



